package com.android.lyricshow.ui;

import java.util.ArrayList;

import com.android.lyricshow.R;
import com.android.lyricshow.adapter.SongAdapter;
import com.android.lyricshow.bean.SongBean;
import com.android.lyricshow.until.AppUntil;
import com.android.lyricshow.until.KEY;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

public class SongFragment extends BaseTitleBarFragment implements OnItemClickListener, View.OnClickListener, ListItemListener {
	

	private ListView mSongList 				= null;
	
	private SongAdapter mListAdapter		= null;
	
	private ArrayList<SongBean> mSongBeans	= null;
	
	@SuppressWarnings("unchecked")
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		Bundle bd = this.getArguments();	
		if(bd != null && bd.containsKey(KEY.SONG_BEAN_LIST)){
			mSongBeans = (ArrayList<SongBean>)bd.getSerializable(KEY.SONG_BEAN_LIST);
		}else{
			mSongBeans = new ArrayList<SongBean>();
		}
		
		mListAdapter= new SongAdapter(sContext, mSongBeans);
		mListAdapter.setClickItemListener(this);
	}
	 
	@Override    
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		super.onCreateView(inflater, container, savedInstanceState);
		
		mSongList	= (ListView)sRootView.findViewById(R.id.all_list);
	
		sTitle = (TextView)sRootView.findViewById(R.id.title_bar_text);
		sTitle.setText(R.string.main_song);
		
		sSettingBtn.setOnClickListener(this);
		
		mSongList.setAdapter(mListAdapter);
		mSongList.setOnItemClickListener(this);
		
		return sRootView;
	}
	
	@Override
	public void onItemClick(AdapterView<?> adapter, View view, int index, long id) {
		
		if(index > mSongBeans.size() - 1){
			AppUntil.showToast(sContext, "The index is out of song list!");
			return;
		}
		
		SongBean sbean = mSongBeans.get(index);
		
		AppUntil.showToast(sContext, "You select " + sbean.getTitle());
		
		Intent it = new Intent(sContext, PlayActivity.class);
		it.putExtra(KEY.SONG_BEAN_LIST, mSongBeans);
		it.putExtra(KEY.CURRENT_INDEX, index);
		
		goToPlay(it);
	}
	
	private void goToPlay(Intent it){
		startActivity(it);
		getActivity().overridePendingTransition (R.anim.push_right_in, R.anim.push_left_out);
	}
	
	@Override
	public void onBackKey(){
		this.getActivity().finish();
	}

	@Override
	public void onClick(View v) {
		switch(v.getId()){
			case R.id.title_bar_setting:
				Intent it = new Intent(sContext, PlayActivity.class);
				it.putExtra(KEY.NOW_PLAYING, 0);
				
				goToPlay(it);
				break;
		}
	}

	@Override
	public void OnItemClick(int pos) {
		((LyricShowMain)this.getActivity()).addToPlayList(mSongBeans.get(pos));
	}
}
